<script setup lang="ts">
import { ref, watch, onMounted } from "vue";
import { useSystemStoreWithOut } from "@/store/modules/system";
import { storeToRefs } from "pinia";
import { getDictList } from "@/api/dict";

const systemStore = useSystemStoreWithOut();
const { materialTypeList } = storeToRefs(systemStore);

const props = defineProps({
  list: {
    type: Array,
    default: () => []
  },
  tableHeight: {
    type: Number,
    default: 0
  }
});

const tableData = ref([]);
const curRow = ref({});
const stockTypeList = ref([]);

watch(
  () => props.list,
  (newVal: any) => {
    console.log(newVal);
    tableData.value = newVal;
  },
  { deep: true }
);

const handleRowClick = (row: any) => {
  curRow.value = row;
};

onMounted(() => {
  getDictList({ dictCode: "STOCK_TYPE" }).then(res => {
    stockTypeList.value = res.data || [];
  });
});
</script>

<template>
  <div>
    <el-table
      class="tableBox"
      :data="tableData"
      style="width: 100%"
      :height="tableHeight"
      @row-click="handleRowClick"
    >
      <el-table-column type="index" label="序号" min-width="80px" />
      <el-table-column
        prop="materialCode"
        label="物料编码"
        min-width="128px"
        show-overflow-tooltip
      />
      <el-table-column
        prop="materialName"
        label="物料名称"
        min-width="120px"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料类型"
        prop="materialType"
        min-width="104px"
        show-overflow-tooltip
      >
        <template #default="scope">
          <el-text>{{
            materialTypeList.find(
              val => val.dictLabelValue == scope.row.materialType
            )?.dictLabelName
          }}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        prop="materialSpecs"
        label="物料规格"
        min-width="200px"
        show-overflow-tooltip
      />
      <el-table-column
        prop="unit"
        label="单位"
        min-width="80px"
        show-overflow-tooltip
      />
      <el-table-column
        prop="incrStockType"
        label="调增库存类型"
        min-width="140px"
        show-overflow-tooltip
      >
        <template #default="scope">
          <el-text>{{
            stockTypeList.find(
              val => val.dictLabelValue == scope.row.incrStockType
            )?.dictLabelName
          }}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        prop="decrStockType"
        label="调减库存类型"
        min-width="140px"
        show-overflow-tooltip
      >
        <template #default="scope">
          <el-text>{{
            stockTypeList.find(
              val => val.dictLabelValue == scope.row.decrStockType
            )?.dictLabelName
          }}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        prop="num"
        label="调整数量"
        min-width="104px"
        show-overflow-tooltip
      />
    </el-table>
  </div>
</template>
<style lang="scss" scoped>
.tableBox {
  .el-input {
    --el-input-width: 100% !important;
  }
}
</style>
